package com.xinz.Android.Tool;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.xinz.Android.Model.Spot;

public class SpotXMLHandler extends DefaultHandler {

	private Spot spot;
	
	private String tempString;
	
	private static final String SPOT = "spot";
	private static final String ID = "id";
	private static final String IMAGE_NAME = "image_name";
	private static final String SPOTPOINT1_IMAGE_NAME = "spotpoint1_image_name";
	private static final String SPOTPOINT1_LEFTTOP_HEIGHT = "spotpoint1_lefttop_height";
	private static final String SPOTPOINT1_LEFTTOP_WIDTH = "spotpoint1_lefttop_width";
	private static final String SPOTPOINT1_HEIGHT = "spotpoint1_height";
	private static final String SPOTPOINT1_WIDTH = "spotpoint1_width";
	
	public Spot getSpot() {
		return spot;
	}
	
	@Override
	public void startDocument() throws SAXException {
		// init
		spot = null;
	}
	
	@Override
	public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
		if(SPOT.equals(localName)) {
			spot = new Spot();
			spot.setId(Integer.parseInt(attributes.getValue(ID)));
		}
		tempString = localName;
	}
	
	@Override
	public void endElement(String uri, String localName, String name) throws SAXException {
		if(SPOT.equals(localName) && spot != null) {
			// do something
		}
		tempString = null;
	}
	
	@Override
	public void characters(char[] ch, int start, int length) {
		if(spot != null) {
			String valueText = new String(ch, start, length);
			if(IMAGE_NAME.equals(tempString)) {
				spot.setImage_name(valueText);
			}
			else if(SPOTPOINT1_IMAGE_NAME.equals(tempString)) {
				spot.setSpotpoint1_image_name(valueText);
			}
			else if(SPOTPOINT1_LEFTTOP_HEIGHT.equals(tempString)) {
				spot.setSpotpoint1_lefttop_height(Integer.valueOf(valueText));
			}
			else if(SPOTPOINT1_LEFTTOP_WIDTH.equals(tempString)) {
				spot.setSpotpoint1_lefttop_width(Integer.valueOf(valueText));
			}
			else if(SPOTPOINT1_HEIGHT.equals(tempString)) {
				spot.setSpotpoint1_height(Integer.valueOf(valueText));
			}
			else if(SPOTPOINT1_WIDTH.equals(tempString)) {
				spot.setSpotpoint1_width(Integer.valueOf(valueText));
			}
		}
	}
}
